<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="120px" inline>
      <el-form-item label="名称" prop="name" required>
        <el-input v-model="form.name" placeholder="请输入名称"/>
      </el-form-item>
      <el-form-item label="背景颜色" prop="color" required>
        <el-select v-model="form.color" placeholder="请选择颜色">
          <el-option label="红色" value="red"></el-option>
          <el-option label="绿色" value="green"></el-option>
          <el-option label="蓝色" value="blue"></el-option>
          <el-option label="紫色" value="violet"></el-option>
          <el-option label="黄色" value="yellow"></el-option>
          <el-option label="橘金色" value="gold"></el-option>
          <el-option label="粉色" value="pink"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="优惠券类型" prop="coupon_type" required>
        <el-select v-model="form.coupon_type" placeholder="请选择优惠券类型">
          <el-option label="满减" :value="10"></el-option>
          <el-option label="折扣" :value="20"></el-option>
        </el-select>

      </el-form-item>
      <el-form-item label="最低消费金额" prop="min_price" required>
        <el-input class="w200" v-model="form.min_price" type="number" placeholder="最低消费金额"/>
      </el-form-item>
      <el-form-item v-if="form.coupon_type == '10'" label="满减金额" prop="reduce_price">
        <el-input v-model="form.reduce_price" type="number" placeholder="满减金额"/>
      </el-form-item>
      <el-form-item v-if="form.coupon_type == '20'" label="折扣率" prop="discount">
        <el-input v-model="form.discount" type="number" placeholder="请输入折扣率,1~10" min="1" max="10"/>
      </el-form-item>
      <el-form-item label="有效期类型" prop="expire_type" required>
        <el-select v-model="form.expire_type" placeholder="请选择有效期类型">
          <el-option label="领取后x天有效" :value="10"></el-option>
          <el-option label="规定时间使用" :value="20"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="有效期天数" prop="expireDay" v-if="form.expire_type == '10'">
        <el-input v-model="form.expire_day" type="number" placeholder="请输入有效期天数"/>
      </el-form-item>
<!--      <el-form-item label="适用商品范围" prop="apply_range" required>
        <el-select v-model="form.apply_range" placeholder="请选择适用商品范围">
          <el-option label="全部商品" :value="10"></el-option>
          <el-option label="指定商品" :value="20"></el-option>
        </el-select>
      </el-form-item>-->
      <el-form-item label="发放数量" prop="total_num" required>
        <el-input class="w200" v-model="form.total_num" type="number" placeholder="请输入发放数量"/>
      </el-form-item>


      <el-form-item label="排序">
        <el-input class="w200" v-model="form.sort" type="number"/>
      </el-form-item>

      <el-form-item label="开始时间" prop="start_time" required>
        <el-date-picker
          v-model="form.start_time"
          type="datetime"
          placeholder="请选择"
          format="yyyy-MM-dd HH:mm:ss"
          style="width: 200px;"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="结束时间" prop="end_time" required>
        <el-date-picker
          v-model="form.end_time"
          type="datetime"
          placeholder="请选择"
          format="yyyy-MM-dd HH:mm:ss"
          style="width: 200px;"
        ></el-date-picker>
      </el-form-item>


      <el-form-item label="状态">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="-1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>


      <el-form-item style="margin-top: 30px">
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button @click="goBack">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import api from '@/api/coupon'

export default {
  components: {},
  data() {
    return {
      form: {
        name: '',
        color: 'red',
        coupon_type: 10,
        reduce_price: 0,
        discount: 0,
        min_price: 0,
        expire_type: 10,
        expire_day: 30,
        start_time: '',
        end_time: '',
        sort: 10,
        apply_range: 10,
        status: 1,
        total_num: 100
      }
    }
  },

  mounted() {
    if (this.$route.query.hasOwnProperty('id')) {
      this.getData(this.$route.query.id)
    }
  },

  methods: {

    getData(id) {
      api.info({ id: id }).then((res) => {
        this.form = res.data.info
      })
    },

    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let req
          if (this.form.id > 0) {
            req = api.update(this.form)
          } else {
            req = api.create(this.form)
          }
          req.then((res) => {
            this.$message({
              type: 'success',
              message: res.msg,
              duration: 500,
              onClose: () => {
                this.$router.go(-1)
              }
            })

          }).catch((error) => {

          })
        }
      })

    }
  }
}
</script>

<style scoped>

</style>

